package com.gjun.struts.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.gjun.struts.bean.GjunMainTypeJson2Bean;
import com.gjun.struts.bean.GjunMaintype;
import com.gjun.struts.resource.GjunMainTypeResourceIf;
import com.gjun.struts.util.JSONHelper;
import com.gjun.struts.util.UUIDGenerator;

public class MgrMainTypeAction {
	private String result;

	private GjunMainTypeResourceIf maintypeResource;
	
	private int id;
	
	private String typename;
	
	private String info;
	
	private File file;

	private String fileFileName;

	private String fileContentType;
	
	private void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src));
				out = new BufferedOutputStream(new FileOutputStream(dst));
				byte[] buffer = new byte[1024];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}
	
	

	

	public String loadAllMainType(){
		List<GjunMainTypeJson2Bean> beans = maintypeResource.loadAllMainTypes();
		try {
			result = JSONHelper.mainTypesToJson2(beans);  
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}
	
	
	public String checkMainTypeExist(){
		boolean isHave = maintypeResource.loadMainTypeByNameAndId(typename, id);
		if(isHave){
			result = "isHave";
		}else{
			result = "notHave";
		}
		return "success";
	}
	public String editMainType(){
		GjunMaintype mt = maintypeResource.loadMainTypeById(id);
		ServletActionContext.getRequest().getSession().setAttribute("edit_GjunMaintype",mt);
		result = "forward_to_edit";
		return "success";
	}
	
	
	public String updateMainType(){
		String filePath = null;
		if(file!=null){
			String fileName = new UUIDGenerator().generate().toString()
					+ getExtention(fileFileName);
			
			String fileSavePath = ServletActionContext.getRequest()
					.getRealPath("/")
					+ "newsImage/" + fileName;
			//System.out.println(ServletActionContext.getRequest().getContextPath()+"/newsImage/"+fileName);
			copy(file,new File(fileSavePath));
	
			
			
			filePath = ServletActionContext.getRequest().getContextPath()+"/newsImage/"+fileName;
		}
		
		try {
			boolean isUpdate = maintypeResource.updateMainType(id, info, typename, filePath);
			if(isUpdate){
				result = "updateSuccess";
			}else{
				result = "updateFaild";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = "updateError";
		}
		return "success";
	}
	public String delMainType(){
		//System.out.println("id:  "+id);
		try {
			boolean isDel = maintypeResource.delMainTypeById(id);
			if(isDel){
				result = "delSuccess";
			}else{
				result = "delFaild";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = "delError";
		}
		
		return "success";
	}
	
	
	public String activeMainTypeNews(){
		try {
			boolean isUpdate = maintypeResource.updateMainTypeNewsStateToActive(id);
			if(isUpdate){
				result = "updateSuccess";
			}else{
				result = "updateFaild";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = "updateError";
		}
		return "success";
	}
	
	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getTypename() {
		return typename;
	}

	public void setTypename(String typename) {
		this.typename = typename;
	}

	public GjunMainTypeResourceIf getMaintypeResource() {
		return maintypeResource;
	}

	public void setMaintypeResource(GjunMainTypeResourceIf maintypeResource) {
		this.maintypeResource = maintypeResource;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
    
	
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
}
